<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>网站管理列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${global_templets_skin}/systemstyle/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${global_templets_skin}/systemstyle/style/admin.css" media="all">
</head>
<body>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>组件</cite></a>
        <a><cite>数据表格</cite></a>
        <a><cite>加入表单元素</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">网站管理</div>
                <div class="layui-card-body">
                    <script type="text/html" id="siteToolBar">
                        <div class="layui-btn-group">
                            <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon">&#xe654;</i>添加</button>
                            <button class="layui-btn layui-btn-sm" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</button>
                            <button class="layui-btn layui-btn-sm" lay-event="refresh"><i class="layui-icon">&#xe666;</i>刷新</button>
                        </div>
                    </script>
                    <table class="layui-hide" id="site-table-form" lay-filter="site-table-form"></table>

                    <script type="text/html" id="status-table-switchTpl">
                        <input type="checkbox" name="status" lay-skin="switch" lay-text="关闭|开启"
                               lay-filter="status-table-switch"
                               value="{{ d.id }}" {{ d.status== 0 ? '' : 'checked' }}>
                    </script>

                    <script type="text/html" id="verifyTpl">
                        {{#  if(d.verify == '1'){ }}
                        <button class="layui-btn layui-btn-normal layui-btn-xs">已审核</button>
                        {{#  } else if(d.verify == '2'){ }}
                        <button class="layui-btn layui-btn-primary layui-btn-xs">未审核</button>
                        {{#  } else{ }}
                        <button class="layui-btn layui-btn-warm layui-btn-xs">处理中</button>
                        {{#  } }}
                    </script>

                    <script type="text/html" id="user-table-setTpl">
                        <a class="layui-btn layui-btn-normal layui-btn-xs" href="/member/site/index.do?id={{ d.id }}" target="_parent"><i class="layui-icon layui-icon-set"></i>管理</a>
                        <a class="layui-btn layui-btn-xs"><i class="layui-icon layui-icon-cart"></i>续费</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="${global_templets_skin}/systemstyle/layui/layui.js"></script>
<script>
    layui.config({
        base: '${global_templets_skin}/systemstyle/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'form'], function () {
        var table = layui.table
            , form = layui.form
            , $ = layui.$;

        table.render({
            elem: '#site-table-form'
            , url: '/member/site/listData.do'
            , cellMinWidth: 80
            , toolbar: '#siteToolBar'
            , cols: [[
                {type: 'numbers'}
                , {type: 'checkbox'}
                , {field: 'siteName', title: '网站名称', align:'center'}
                , {field: 'expireTime', title: '到期时间', align:'center', minWidth: 120, sort: true}
                , {field: 'status', title: '网站状态', align:'center', templet: '#status-table-switchTpl'}
                , {field: 'verify', title: '审核', align:'center', templet: '#verifyTpl'}
                , {fixed: 'right', title: '操作', align:'center', toolbar: '#user-table-setTpl'}
            ]]
            ,id: 'siteReload'
            , page: true
        });

        //监听开关操作
        form.on('switch(status-table-switch)', function (obj) {
            $.ajax({
                url: "/member/site/updateStatus.do",
                data: {'id': this.value, 'status': obj.elem.checked},
                type: "post",
                dataType: 'json',
                cache: false,
                async: false,
                error: function (i, g, h) {
                    layer.msg('发送错误', {icon: 2});
                },
                success: function (data) {
                    if(data.code == '0'){
                        obj.elem.checked = checked;
                    }else {
                        obj.elem.checked = !checked;
                    }
                    form.render();
                },
                error: function (data) {
                    alert("出错：" + siteCompanyService);
                }
            });
        });

        table.on('toolbar', function(obj){
            var checkStatus = table.checkStatus('siteReload');
            var data = checkStatus.data;
            if(obj.event === 'add'){
                if(data.length > 1){
                    layer.msg("请选择一行作为父级菜单");
                }else{
                    layer.open({
                        type: 2
                        ,title: '添加网站'
                        ,content: "/member/site/add.do"
                        ,maxmin: true
                        ,area: ['500px', '350px']
                        ,btn: ['确定', '取消']
                        ,style: 'background-color:#fff;'
                        ,yes: function(index, layero){
                            var iframeWindow = window['layui-layer-iframe'+ index]
                                ,submitID = 'LAY-user-front-submit'
                                ,submit = layero.find('iframe').contents().find('#'+ submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url:'/member/site/add.do',
                                    type:'post',
                                    dataType: 'json',
                                    data: data.field,
                                    success:function(e){
                                        if (e.code == "0") {
                                            layer.msg(e.message);
                                            table.reload('siteReload');
                                            return;
                                        }else {
                                            layer.msg(e.message);
                                            return;
                                        }
                                    }
                                })
                                layer.close(index); //关闭弹层
                            });
                            submit.trigger('click');
                        }
                    });
                    // })
                }
            }else if(obj.event === "delete"){
                deList=[];
                data.forEach(function(n,i){
                    deList.push(n.id);
                })
                if(data.length < 1){
                    layer.msg("请选择一行进行删除！");
                }else{
                    layer.confirm('真的删除选定行的网站吗?', function(index){
                        $.ajax({
                            type: "POST"
                            ,dataType: 'json'
                            , url: '/member/site/del.do' //实际使用请改成服务端真实接口
                            , data: "ids=" + deList
                            , success: function (e) {
                                if (e.code == "0") {
                                    layer.msg('已删除', {icon: 1});
                                    layer.close(index); //关闭弹层
                                    table.reload('siteReload');
                                }else {
                                    layer.msg(e.message);
                                    return;
                                }
                            }
                        });

                    });
                }
            }else if(obj.event === "refresh"){
                table.reload('siteReload');
            }
        })

        //监听性别操作
        form.on('switch(status-table-switch)', function(obj){
            if(obj.elem.checked == true){
                layer.tips("开启网站后请进入管理后台设置网站模板后才能正常浏览", obj.othis);
            }
        });


    });
</script>
</body>
</html>